package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.Products;
import com.wanshi.service.impl.ProductsServiceImpl;
import com.wanshi.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-13
 */
@CrossOrigin
@RestController
@RequestMapping("/products")
@Api(value = "测试SwaggerAPI Annotation", tags = {"测试SwaggerAPI"})
public class ProductsController {

    @Autowired
    public ProductsServiceImpl productsService;

    @ApiOperation(value = "一个参数的测试", notes = "一个参数的测试")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="id", value="id", dataTypeClass = Integer.class, required = true)
            }
    )
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return Result.ok(productsService.getById(id));
    }

    @ApiOperation(value = "三个参数的测试", notes = "三个参数的测试")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="ye", value="页数", paramType = "path",required = true),
                    @ApiImplicitParam(name="size", value="一页显示多少条", paramType = "path", required = true),
            }
    )
    @PostMapping("/findPageAndCount/{ye}/{size}")
    public Result findPageAndCount(@PathVariable int ye,@PathVariable int size,@RequestBody Products products){
        Page page = new Page(ye,size);
        QueryWrapper<Products> wrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(products.getName())){
            wrapper.like("name", products.getName());
        }
        if(!StringUtils.isEmpty(products.getTypeid())){
            wrapper.eq("typeid", products.getTypeid());
        }
        if(!StringUtils.isEmpty(products.getState())){
            wrapper.eq("state", products.getState());
        }
        wrapper.orderByDesc("id");
        productsService.page(page,wrapper);
        return Result.ok(page);
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name="id", value="用户id",dataType = "String",paramType = "path",required = true)
    )
    @DeleteMapping("/id/{id}")
    public  Result delete(@PathVariable String id){
        productsService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("修改用户")
    @PutMapping
    public Result update(@RequestBody Products products){
        productsService.updateById(products);
        return Result.ok();
    }

    @ApiOperation("添加用户")
    @PostMapping("/products")
    public Result addUser(@RequestBody Products products) throws ParseException {

        productsService.saveOrUpdate(products);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/delBatch")
    public Result delBatch(@RequestBody List<String> ids){
        productsService.removeByIds(ids);
        return Result.ok();
    }

}

